package com.bstek;

import com.bstek.entity.ItemEntity;
import com.bstek.entity.Student;
import com.bstek.entity.StudentMix;
import com.bstek.urule.Utils;
import com.bstek.urule.runtime.KnowledgePackage;
import com.bstek.urule.runtime.KnowledgeSession;
import com.bstek.urule.runtime.KnowledgeSessionFactory;
import com.bstek.urule.runtime.response.RuleExecutionResponse;
import com.bstek.urule.runtime.service.KnowledgeService;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;


@RestController
@RequestMapping("rule")
public class RuleController {

	@RequestMapping("/ageRule")
    public Result rule(@RequestParam String age,@RequestParam String name) throws IOException {
    	//创建一个KnowledgeSession对象
        KnowledgeService knowledgeService = (KnowledgeService) Utils.getApplicationContext().getBean(KnowledgeService.BEAN_ID);
        KnowledgePackage knowledgePackage = knowledgeService.getKnowledge("edas/user_package");
        KnowledgeSession session = KnowledgeSessionFactory.newKnowledgeSession(knowledgePackage);

        Map<String, Object> param = new HashMap<>();
        param.put("age", age);
        param.put("name",name);
        RuleExecutionResponse result = session.fireRules(param);
        System.out.println("年龄：" + session.getParameter("age"));
        System.out.println(session.getParameter("message"));

        return new Result("0",session.getParameter("message").toString());
    }

    @RequestMapping("/duplicateRule")
    public Result duplicateRule() throws IOException {

        //创建一个KnowledgeSession对象
        KnowledgeService knowledgeService = (KnowledgeService) Utils.getApplicationContext().getBean(KnowledgeService.BEAN_ID);
        KnowledgePackage knowledgePackage = knowledgeService.getKnowledge("edas/duplicate_package");
        KnowledgeSession session = KnowledgeSessionFactory.newKnowledgeSession(knowledgePackage);


        Student student = new Student();
        student.setId(333);
        student.setName("ccc");
        Student.Course course2 = new Student.Course();
        course2.setKey("3c3c");
        Student.Message message2 = new Student.Message();
        message2.setTeacher("t33");
        message2.setTime(333333);
        course2.setMessage(message2);
        student.setCourse(course2);

        session.insert(student);

        Student student1 = new Student();
        student1.setId(111);
        student1.setName("aaa");
        Student.Course course1 = new Student.Course();
        course1.setKey("1a1a");
        Student.Message message1 = new Student.Message();
        message1.setTeacher("t11");
        message1.setTime(111111);
        course1.setMessage(message1);
        student1.setCourse(course1);

        StudentMix mix = new StudentMix();
        mix.setStudent(student);
        mix.setStudent1(student1);

        session.insert(mix);

        RuleExecutionResponse result = session.fireRules();

        return new Result("0","");
    }

    @RequestMapping("/onlyRule")
    public Result onlyRule() throws IOException {

        //创建一个KnowledgeSession对象
        KnowledgeService knowledgeService = (KnowledgeService) Utils.getApplicationContext().getBean(KnowledgeService.BEAN_ID);
        KnowledgePackage knowledgePackage = knowledgeService.getKnowledge("edas/only_package");
        KnowledgeSession session = KnowledgeSessionFactory.newKnowledgeSession(knowledgePackage);


        Student student = new Student();
        student.setId(333);
        student.setName("ccc");
        Student.Course course2 = new Student.Course();
        course2.setKey("3c3c");
        Student.Message message2 = new Student.Message();
        message2.setTeacher("t33");
        message2.setTime(333333);
        course2.setMessage(message2);
        student.setCourse(course2);

        session.insert(student);

        RuleExecutionResponse result = session.fireRules();

        return new Result("0","");
    }


    @PostMapping("/item/dto")
    public Result rule(@RequestBody ItemEntity entity) throws IOException {
        //创建一个KnowledgeSession对象
        KnowledgeService knowledgeService = (KnowledgeService) Utils.getApplicationContext().getBean(KnowledgeService.BEAN_ID);
        KnowledgePackage knowledgePackage = knowledgeService.getKnowledge("11/item_check");
        KnowledgeSession session = KnowledgeSessionFactory.newKnowledgeSession(knowledgePackage);
        session.insert(entity);

        RuleExecutionResponse result = session.fireRules();
        result.getMatchedRules().stream().forEach(i-> System.out.println("\n执行的规则名---------------      "+i.getName()));
        return new Result("1","success");
    }
}
